package io.adrainty.bolee.security.feign.impl;

import io.adrainty.bolee.framework.commons.basic.ResultDTO;
import io.adrainty.bolee.framework.commons.user.DataSecurityEntity;
import io.adrainty.bolee.framework.commons.user.UserEntity;
import io.adrainty.bolee.framework.commons.utils.ResultDTOUtil;
import io.adrainty.bolee.security.feign.IUserFeign;
import io.adrainty.bolee.security.models.entity.ResourceEntity;
import io.adrainty.bolee.security.models.entity.RoleEntity;
import io.adrainty.bolee.security.models.request.QueryDataSecurityDTO;
import io.adrainty.bolee.security.models.valobj.UserTypeEnum;
import io.adrainty.bolee.security.service.IResourceService;
import io.adrainty.bolee.security.service.IRoleService;
import io.adrainty.bolee.security.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>UserFeignImpl</p>
 *
 * @author AdRainty
 * @version V1.0.0
 * @description IUserFeign
 * @since 2025/8/7 11:22:53
 */

@Slf4j
@RestController
@Tag(name = "用户接口")
@RequestMapping("/user-feign")
public class UserFeignImpl implements IUserFeign {

    @Resource
    private IUserService userService;

    @Resource
    private IRoleService roleService;

    @Resource
    private IResourceService resourceService;

    @Override
    @Operation(summary = "按照客户查找用户")
    @PostMapping("/username-login/{username}/{company}")
    public ResultDTO<UserEntity> usernameLogin(@PathVariable("username") String username, @PathVariable("company") String company) {
        UserEntity userEntity = userService.findUserByUsername(UserTypeEnum.USER, username, company);
        return ResultDTOUtil.success(userEntity);
    }

    @Override
    @Operation(summary = "手机登录")
    @PostMapping("/mobile-login/{mobile}/{company}")
    public ResultDTO<UserEntity> mobileLogin(@PathVariable("mobile") String mobile, @PathVariable("company") String company) {
        UserEntity userEntity = userService.findUserByMobile(UserTypeEnum.USER, mobile, company);
        return ResultDTOUtil.success(userEntity);
    }

    @Override
    @Operation(summary = "微信登录")
    @PostMapping("/wechat-login/{openId}/{company}")
    public ResultDTO<UserEntity> wechatLogin(@PathVariable("openId") String openId, @PathVariable("company") String company) {
        UserEntity userEntity = userService.findUserByOpenId(UserTypeEnum.USER, openId, company);
        return ResultDTOUtil.success(userEntity);
    }

    @Override
    @Operation(summary = "查找用户所有角色")
    @PostMapping("/find-role-user/{userId}")
    public ResultDTO<List<RoleEntity>> findRoleByUserId(@PathVariable("userId") Long userId) {
        List<RoleEntity> roleEntities = roleService.findRoleByUserId(userId);
        return ResultDTOUtil.success(roleEntities);
    }

    @Override
    @Operation(summary = "按照用户查找资源")
    @PostMapping("/find-resource-user/{userId}")
    public ResultDTO<List<ResourceEntity>> findResourceByUserId(@PathVariable("userId") Long userId) {
        List<ResourceEntity> resourceEntities = resourceService.findResourceByUserId(userId);
        return ResultDTOUtil.success(resourceEntities);
    }

    @Override
    @Operation(summary = "用户数据权限")
    @PostMapping(value = "/user-data-security")
    public ResultDTO<DataSecurityEntity> userDataSecurity(@RequestBody QueryDataSecurityDTO queryDataSecurityVO) {
        DataSecurityEntity securityEntity = userService.findUserDataSecurity(queryDataSecurityVO.getUserId(), queryDataSecurityVO.getRoleVOs());
        return ResultDTOUtil.success(securityEntity);
    }

    @Override
    @Operation(summary = "查询用户")
    @PostMapping(value = "/find-user-by-id")
    public ResultDTO<UserEntity> findUserById(@RequestParam("userId") Long userId) {
        return null;
    }
}
